/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog'
], function ($, common, ready, Collection, template, Model, dialog) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'page_setting',
                condition: {},
                model: {
                    url: _global.url.api + 'page_setting',
                    event: {
                        '.item-edit:click': 'edit',
                        '.item-remove:click': 'remove',
                    },
                    edit: function (e) {
                        _this.data = this.data;
                        var model = this;
                        e.preventDefault();
                        dialog({
                            'title': '修改参数',
                            'content': template('add-tpl', this.data),
                            'width': 600,
                            ok: function () {
                                _this.data.type = $("input[name='type']:checked").val();
                                if (_this.data.type == '' || _this.data.type == undefined) {
                                    Alert({msg: '请选择类型', type: 'error'});
                                    return false;
                                }
                                _this.data.key = $("input[name='key']").val();
                                if (_this.data.key == '' || _this.data.key == undefined) {
                                    Alert({msg: '请填写key', type: 'error'});
                                    return false;
                                }
                                _this.data.imgs = _this.data.type == 1 ? $("input[name='imgs']").val() : 0;
                                _this.data.intro = $("input[name='intro']").val();
                                _this.data.copy = _this.data.type == 2 ? $("input[name='copy']").val() : '';
                                _this.data.url = _this.data.type == 3 ? $("input[name='url']").val() : '';
                                _this.data.id = _this.data.id;
                                if (model.put()) {
                                    _this.collection.fetch();
                                }
                                ;
                            }, cancel: function () {
                            },
                        }).show(e.target);

                        $('input[name=type]').on('change', function () {
                            var type = $(this).val();
                            if (type == 1) {
                                $('#img-item').show();
                                $('#copy-item').hide();
                                $('#url-item').hide();
                            } else if (type == 2) {
                                $('#img-item').hide();
                                $('#copy-item').show();
                                $('#url-item').hide();
                            } else if (type == 3) {
                                $('#img-item').hide();
                                $('#copy-item').hide();
                                $('#url-item').show();
                            }
                        });
                    },
                    remove: function (e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            'content': '确定删除吗?',
                            ok: function () {
                                if (model.del()) {
                                    _this.collection.fetch();
                                }
                                ;
                            }, cancel: function () {
                            }
                        }).show(e.target);
                    },

                },
                callback: function () {
                    loaded();
                },

            });
            this.collection.fetch();
        },

        event: function () {
            this.data = {};
            var _this = this;
            //保存
            $('.btn-add').on('click', function (e) {
                _this.data = {};
                e.preventDefault();
                _this.dialog = dialog({
                    title: '添加页面',
                    content: template('add-tpl', {}),
                    width: 600,
                    ok: function () {
                        _this.data.type = $("input[name='type']:checked").val();
                        if (_this.data.type == '' || _this.data.type == undefined) {
                            Alert({msg: '请选择类型', type: 'error'});
                            return false;
                        }
                        _this.data.key = $("input[name='key']").val();
                        if (_this.data.key == '' || _this.data.key == undefined) {
                            Alert({msg: '请填写key', type: 'error'});
                            return false;
                        }
                        _this.data.imgs = _this.data.type == 1 ? $("input[name='imgs']").val() : 0;
                        _this.data.intro = $("input[name='intro']").val();
                        _this.data.copy = _this.data.type == 2 ? $("input[name='copy']").val() : '';
                        _this.data.url = _this.data.type == 3 ? $("input[name='url']").val() : '';
                        if (new Model({
                                url: _global.url.api + 'page_setting',
                                data: _this.data
                            }).post()) {
                            _this.collection.reload();
                        }
                    }, cancel: function () {
                    }
                });
                _this.dialog.show();
                $('input[name=type]').on('change', function () {
                    var type = $(this).val();
                    if (type == 1) {
                        $('#img-item').show();
                        $('#copy-item').hide();
                        $('#url-item').hide();
                    } else if (type == 2) {
                        $('#img-item').hide();
                        $('#copy-item').show();
                        $('#url-item').hide();
                    } else if (type == 3) {
                        $('#img-item').hide();
                        $('#copy-item').hide();
                        $('#url-item').show();
                    }
                });
            });

            //上传图片
            $('body').on('click', '#btn-upload', function (e) {
                _this.upload_dialog = dialog({
                    content: template('upload-tpl', {}),
                    width: 550,
                    ok: function () {
                        console.log('contentWindow', $("#upload-frame")[0].contentWindow.image);
                        var img = $("#upload-frame")[0].contentWindow.image.get();
                        _this.imgPreview(img);
                    }, cancel: function () {
                    }
                });
                _this.upload_dialog.show();
            });
        },
        imgPreview: function (img) {
            console.log('this.data', this.data);
            if (typeof img.id !== 'undefined') {
                this.data.img = img.id;
                $('#img-view').html(template('img-tpl', img));
            }
        }
    };

    new index();

});